package yace;

import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by yangluhua on 2016/9/23 0023.
 */
public class YaCeThread extends Thread{
    private int threadId ;

    public YaCeThread(int threadId) {
        this.threadId = threadId;
    }

    //get请求压测
    @Override
    public void run() {
        //连接池
        HttpClientPoolUtil.get("http://localhost:8070/trip8080_mobile_monitor/changtu/hello");
        //普通http
        //HttpRequestUtils.get("http://localhost:8070/trip8080_mobile_monitor/changtu/hello", "utf-8");
    }

    /*//post请求压测
    Map map = null;
    @Override
    public void run() {
         Map map = new HashMap<String, String>();
         map.put("name",threadId+"");
        *//*try {
            //连接池
            HttpClientPoolUtil.post("http://localhost:8070/trip8080_mobile_monitor/changtu/hello",new HashMap<String, Object>());
        }catch (Exception e){
            e.printStackTrace();
        }*//*
        //普通http
        HttpRequestUtils.post("http://localhost:8070/trip8080_mobile_monitor/changtu/hello", map, "utf-8");
    }*/

    //普通http请求压测文件上传
    /*@Override
    public void run() {
        String end = "\r\n";
        String twoHyphens = "--";
        String boundary = "*****";
        String newName = threadId+".txt";
        String uploadFile = "E:/springUpload/testRang/"+newName;
        String actionUrl = "http://127.0.0.1:8070/trip8080_mobile_monitor/rang/upload";
        try {
            URL url = new URL(actionUrl);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            //断点续传
            //con.setRequestProperty("Content-Range","bytes 4-10/10");
      *//* 允许Input、Output，不使用Cache *//*
            con.setDoInput(true);
            con.setDoOutput(true);
            con.setUseCaches(false);
      *//* 设置传送的method=POST *//*
            con.setRequestMethod("POST");
      *//* setRequestProperty *//*
            con.setRequestProperty("Connection", "Keep-Alive");
            con.setRequestProperty("Charset", "UTF-8");
            con.setRequestProperty("Content-Type","multipart/form-data;boundary=" + boundary);
      *//* 设置DataOutputStream *//*
            DataOutputStream ds = new DataOutputStream(con.getOutputStream());
            ds.writeBytes(twoHyphens + boundary + end);
            ds.writeBytes("Content-Disposition: form-data; "+ "name=\"myFile\";filename=\"" + newName + "\"" + end);
            ds.writeBytes(end);
      *//* 取得文件的FileInputStream *//*
            FileInputStream fStream = new FileInputStream(uploadFile);
      *//* 设置每次写入1024bytes *//*
            int bufferSize = 1024;
            byte[] buffer = new byte[bufferSize];
            int length = -1;
      *//* 从文件读取数据至缓冲区 *//*
            while ((length = fStream.read(buffer)) != -1) {
        *//* 将资料写入DataOutputStream中 *//*
                ds.write(buffer, 0, length);
            }
            ds.writeBytes(end);
            ds.writeBytes(twoHyphens + boundary + twoHyphens + end);
      *//* close streams *//*
            fStream.close();
            ds.flush();
      *//* 取得Response内容 *//*
            InputStream is = con.getInputStream();
            int ch;
            StringBuffer b = new StringBuffer();
            while ((ch = is.read()) != -1) {
                b.append((char) ch);
            }
      *//* 将Response显示于Dialog *//*
            System.out.println("上传成功" + b.toString().trim());
      *//* 关闭DataOutputStream *//*
            ds.close();
        } catch (Exception e) {
            System.out.println("上传失败" + e);
        }
    }*/
}
